package com.oocl.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.oocl.dao.OrdersDao;
import com.oocl.dao.base.BaseJPADaoUtil;
import com.oocl.entity.Orders;


@Repository("ordersDao")
public class OrdersDaoImpl extends BaseJPADaoUtil implements OrdersDao{

	
	/*
     * @Method  findOrderByOid
     * @function load Orders by   oid
     * @param  oid  Orders oid
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrderByOid(String oid) {
		String sql = "select o from Orders o left join fetch o.food f  left join fetch o.merchant m left join fetch o.client c  where o.oid=:oid ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("oid", oid)
		                       .getResultList();
		System.out.println(oid);
		System.out.println(list.get(0).getClient().getCname());
		
		return list;
	}

	/*
     * @Method  findOrdersByOstate
     * @function load Orders by  ostate
     * @param  ostate  Orders ostate
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrdersByOstate(String ostate) {
		String sql = "select o from Orders o left join fetch o.food f  left join fetch o.merchant m left join fetch o.client c  where o.ostate=:ostate ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("ostate", ostate)
		                       .getResultList();
		return list;
	}
  
	/*
     * @Method  findOrdersByCid
     * @function load Orders by cid
     * @param  cid  Clients cid
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrdersByCid(String cid) {
		String sql = "select o from Orders o left join fetch o.food f  left join fetch o.merchant m left join fetch o.client c  where oc.cid=:cid ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("cid", cid)
		                       .getResultList();
		return list;
	}
     
	/*
     * @Method  findOrdersByIcnum
     * @function load Orders by   icnum
     * @param  icnum  Merchants  icnum
     * @return list Orders List
     * @author garen
     * @date 2017-8-12
     * */
	public List<Orders> findOrdersByIcnum(String icnum) {
		String sql = "select o from Orders o left join fetch o.food f  left join fetch o.merchant m left join fetch o.client c left join fetch o.deliverInfo d  where m.icnum=:icnum ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("icnum", icnum)
		                       .getResultList();
		return list;
	}
	
	/*
     * @Method  getOrdersByIcnum
     * @function load Orders by   icnum
     * @param  icnum  Merchants  icnum
     * @return list distinct Orders List
     * @author lionel
     * @date 2017-8-15
     * */
	public List<Orders> getOrdersByIcnum(String icnum) {
		String sql = "select o from Orders o left join fetch o.food f  left join fetch o.merchant m left join fetch o.client c left join fetch o.deliverInfo d where m.icnum=:icnum ";
		List<Orders> list =  entityManager.createQuery(sql)
		                       .setParameter("icnum", icnum)
		                       .getResultList();
		Map<String, Orders> map=new HashMap<String, Orders>();
		for (Orders o:list) {
			map.put(o.getOid(), o);
		}
		list.clear();
		for (String oid:map.keySet()) {
			list.add(map.get(oid));
		}
		return list;
	}


   
}
